﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Configuration;
using FwProject.Controllers;
using FwProject.Product;
using FwProject.Shop;
using FwProject.Shop.Dto;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Configuration;

namespace FwProject.Web.Shop.Controllers
{
    [AbpMvcAuthorize]
    public class ShopTypeController : FwProjectControllerBase
    {
        private readonly IShopTypeAppService _shopTypeAppService;
        private readonly IConfigurationRoot _appConfiguration;
        private readonly IProductTypeAppService _productTypeAppService;
        public ShopTypeController(IShopTypeAppService shopTypeAppService, IHostingEnvironment env, IProductTypeAppService productTypeAppService)
        {
            _shopTypeAppService = shopTypeAppService;
            _appConfiguration = env.GetAppConfiguration();
            _productTypeAppService = productTypeAppService;
        }
        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<JsonResult> GetShopTypeList(string keyword = "")
        {
            var model = await _shopTypeAppService.GetPagedListAsync(keyword);

            return Json(model);
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<JsonResult> GetShopTypeList2(Guid id, string keyword = "")
        {
            var model = await _shopTypeAppService.GetPagedList2Async(id, keyword);

            return Json(model);
        }

        public async Task<IActionResult> Edit(Guid id)
        {
            ViewBag.ImageUrl = _appConfiguration["ImageUrl:url"].ToString();
            var model = await _shopTypeAppService.GetByIdEnterAsync(id);
            if (model.Parentid.HasValue)
            {
                var list = await _shopTypeAppService.GetParentListAsync();
                ViewBag.Parent = list.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.TypeName });
            }
            var productTypeName = (await _productTypeAppService.GetEnterDtoAsync(model.ProductTypeid))?.TypeName;
            ViewBag.ProductTypeName = productTypeName;
            ViewBag.Id = id;
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Guid id, ShopTypeInput input)
        {
            await _shopTypeAppService.ModifyAsync(id, input);
            return Json(id);
        }

        public async Task<IActionResult> Create(bool istop = false)
        {
            ViewBag.ImageUrl = _appConfiguration["ImageUrl:url"].ToString();
            ViewBag.IsTop = istop;
            if (!istop)
            {
                var list = await _shopTypeAppService.GetParentListAsync();
                ViewBag.Parent = list.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.TypeName });
            }

            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(ShopTypeInput input)
        {
            var id = await _shopTypeAppService.CreateAsync(input);
            return Json(id);
        }


        public async Task<IActionResult> Delete(string ids)
        {
            await _shopTypeAppService.DeleteAsync(ids);
            return Json("成功");
        }


        public async Task<IActionResult> GetChildTypeData(Guid parentid)
        {
            var data = await _shopTypeAppService.GetChildListByParentIdAsync(parentid);
            return Json(data);
        }

        public async Task<IActionResult> SwitchActive(Guid id, bool active)
        {
            var t = await _shopTypeAppService.SwitchActive(id, active);
            return Json(t);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadImage()
        {
            string FileServicePath = _appConfiguration["FileServicePath:TempShopType"].ToString();
            return Json(base.UploadFile(FileServicePath));
        }

        public async Task<IActionResult> GetTypeJson(Guid? id,string keyword="")
        {
            var list = await _shopTypeAppService.GetListAsync(id,keyword);
            return Json(list);
        }

    }
}